/**
 * @Author leiyuan
 * @Desc 公共JS
 * @Date 2021-12-03
 */
let isDev = false

// http request 拦截器
axios.interceptors.request.use(
    config => {
        if (localStorage.token) { //判断token是否存在
            config.headers.Authorization = localStorage.token;  //将token设置成请求头
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);

// http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.status == 401) {
            console.error("token 已过期")
            this.$info({
                title: '提示',
                content: h('div', {}, [
                    h('p', '会话超时，请重新登录'),
                ]),
                onOk() {
                    let lt = parent.location || location
                    lt.href = "/"
                },
            });
        }
        return response;
    },
    error => {
        return Promise.reject(error);
    }
);

function jsonPost(url, data) {
    return axios({
        method: 'post',
        url: url,
        data: data,
        headers: {'Content-Type': 'application/json'},
    })
}

async function jsonPostSyn(url, data) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            data: data,
            headers: {'Content-Type': 'application/json'},
        }).then(res => {
            resolve(res.data)
        }).catch(err =>{
            reject(err.data)
        })
    })
}

function getReq(url, params){
    return axios.get(url, {
        params: params
    })
}

function delReq(url, data){
    return axios.delete(url, {
        data: data
    })
}